package query.select

import ast.statement.select.SqlSelect
import ast.statement.select.SqlSelectQuery
import ast.statement.select.SqlValuesSelect
import database.DBConnection
import expr.DB
import expr.Query
import util.toSqlString
import visitor.getExpr
import visitor.getQueryExpr
import java.sql.Connection

class ValuesSelect(
    var db: DB = DB.MYSQL,
    override var conn: Connection? = null,
    override var isTransaction: Boolean = false,
    override var dbConnection: DBConnection? = null
) : SelectQueryImpl() {
    val valuesSelect = SqlValuesSelect()

    override fun getSelect(): SqlSelectQuery = this.valuesSelect

    override fun getDbType(): DB = this.db

    override fun sql(): String = toSqlString(valuesSelect, db)

    fun addRow(row: List<Any>): ValuesSelect {
        val addRow = row.map {
            when (it) {
                is Query -> getQueryExpr(it, this.db).expr
                else -> getExpr(it)
            }
        }

        valuesSelect.values.add(addRow)

        return this
    }
}